<template>
    <div>
        <center><h3>密码修改</h3></center>
        <el-form ref="updateForm"  :model="patient"  :rules="rules" label-width="80px" class="login-box">
            <el-form-item label="原密码"  class="out" prop="oldpassword" >
                <el-input v-model="patient.oldpassword" type="password" class="inputs" placeholder="请输入原密码"  />
            </el-form-item>
            <el-form-item label="新密码"  class="out" prop="password" >
                <el-input v-model="patient.password" type="password" class="inputs" placeholder="请输入新密码"  />
            </el-form-item>
            <el-form-item label="确认密码"  class="out" prop="passwords"  >
                <el-input v-model="patient.passwords" type="password" class="inputs" placeholder="请再次输入密码"  />
            </el-form-item>
            <center>
                <el-form-item>
                    <el-button type="primary" @click="OnSubmit('updateForm')" >确认修改</el-button>
                </el-form-item>
            </center>
        </el-form>
    </div>
</template>

<script>
    import axios from "axios";
    import {ElMessage} from "element-plus";

    export default {
        name: "PChangePassword",
        data(){
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    // if (this.user.passwords!== '') {
                    //     this.$refs.user.validateField('passwords');
                    // }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.patient.password) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return{
                patient: {
                    deleted:0,
                    gmtCreate:'',
                    gmtUpdate:'',
                    id: '',
                    name: '',
                    idcard:'',
                    sex:'',
                    age:'',
                    login_name:'',
                    address:'',
                    password:'',
                    passwords:'',
                    phone:'',
                    oldpassword:'',
                },
                rules: {
                    password: [
                        {required:true,message:"密码不可为空",trigger:'blur'},
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    passwords: [
                        {required:true,message:"第二次密码不可为空",trigger:'blur'},
                        { validator: validatePass2, trigger: 'blur' }
                    ],
                    oldpassword: [
                        {required:true,message:"原密码不可为空",trigger:'blur'},
                    ],
                }
            }
        },

        methods: {
            OnSubmit(formName){
                this.$refs[formName].validate((valid)=>{
                    if(valid){

                        axios.post("http://119.29.60.153:8001/doctorservice/patient-info/updatePassword?id="+localStorage.getItem("id")+"&old="+this.patient.oldpassword+"&update="+this.patient.password).then(resp=> {
                            if(resp.data.message=='成功'){
                                alert('修改成功！')
                                location.reload();
                            }
                            else {
                                ElMessage.error(resp.data.message);

                            }
                        })

                    }else {
                        return false;
                    }
                })

            },

        }
    }
</script>

<style scoped>
    .login-box{

        padding: 10px 35px 15px 35px;

    }
    .out{
        margin-left: 350px;
    }
    .inputs{
        width: 500px;
    }
</style>